package cn.edu.tsinghua.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateConverter {

	public static Date getDate(String dateString) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		try {
			date = dateFormat.parse(dateString);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}
	
	
	public static int getIntervalDays(Date fDate,Date oDate) {
		if(null == fDate || null == oDate) {
			return -1;
		}
		long intervalMilli = oDate.getTime() - fDate.getTime();
		return (int)(intervalMilli /(24*60*60*1000));
	}
	
	public static int getDaysOfTwoDate(Date fDate,Date oDate) {
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(fDate);
		
		int day1 = calendar.get(Calendar.DAY_OF_YEAR);
		
		calendar.setTime(oDate);
		int day2 = calendar.get(Calendar.DAY_OF_YEAR);
		
		return day2-day1;
		
		
	}
	
	public static String getNowDateString() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		String dateString = simpleDateFormat.format(new Date());
		return dateString;
	}
	
	public static String getNowDateNoTimeString() {
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String dateString = simpleDateFormat.format(new Date());
		return dateString;
	}
	
	public static void main(String[] args) {
		System.out.println(getNowDateNoTimeString());
		
		
	}
	
}
